package com.apptest.apptest;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

public class SecondActivity extends AppCompatActivity {

    private static final String EXTRA_PHONE = "EXTRA_PHONE";
    private static final String EXTRA_PWD = "EXTRA_PWD";


    private TextView mTxtResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        mTxtResult = findViewById(R.id.txt_result);

        //获取从第一个页面传过的值
        String phone = getIntent().getStringExtra(EXTRA_PHONE);
        String pwd = getIntent().getStringExtra(EXTRA_PWD);


        StringBuffer buffer = new StringBuffer();
        buffer.append("用户名：").append(phone).append("\n").append("密码：").append(pwd);


        mTxtResult.setText(buffer.toString());


        findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView textView = findViewById(R.id.et_back_result);
                String result = textView.getText().toString().trim();

                //从第二个页面回调给第一个页面的值
                Intent intent = new Intent();
                intent.putExtra("EXTRA_RESULT", result);
                setResult(RESULT_OK, intent);
                SecondActivity.this.finish();


            }
        });

    }

    public static void launch(Context context, String phone, String pwd) {
        Intent intent = new Intent(context, SecondActivity.class);
        intent.putExtra(EXTRA_PHONE, phone);
        intent.putExtra(EXTRA_PWD, pwd);
        //若需要从第二个页面给第一个页面返回值，可使用startActivityForResult方法启动，否则，使用startActivity方法
        ((Activity) context).startActivityForResult(intent, 100);

    }
}
